package com.gm.service.user;

import com.gm.dao.bill.IBillDao;
import com.gm.dao.customer.ICustomerDao;
import com.gm.dao.rate.IRateDao;
import com.gm.dao.remindPayTask.IRemindPayTaskDao;
import com.gm.model.po.*;
import com.gm.tools.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * Created by Administrator on 2017/6/28.
 */
@Transactional
@Service
public class TaskService {
    private static  final Logger logger = LoggerFactory.getLogger(TaskService.class);

    @Autowired
    private IBillDao billDao;
    @Autowired
    private IRateDao rateDao;
    @Autowired
    private ICustomerDao customerDao;
    @Autowired
    private IRemindPayTaskDao remindPayTaskDao;

    public void opertationTask(List<Card> cardList){
        logger.info("=========Method[opertationTask]==========");
        try {
            for(int i = 0; i < cardList.size(); i++ ){
                Card card = cardList.get(i);
                logger.info("Method[opertationTask],card.getId[{}],card.getId()",card.getId());
                int billNum = billDao.exsitCardBill(card.getId(), card.getStmStatementDate());
                logger.info("Method[opertationTask],billNum[{}]",billNum);
                if(billNum == 0) {
                    Bill bill = new Bill();
                    bill.setRepayMoney(card.getRelLimit());//信用卡额度
                    bill.setCtmid(card.getCid());//客户ID
                    bill.setCdid(card.getId());//卡ID
                    bill.setStatus(0);//0 未处理 1已处理
                    bill.setUpdateTime(DateUtils.getCurrentUnixTime());
                    bill.setCreateTime(DateUtils.getCurrentUnixTime());
                    Customer customer = customerDao.selectByPrimaryKey(card.getCid());//获取客户信息
                    Rate rate = rateDao.selectByPrimaryKey(customer.getRid());//获取客户手续费
                    bill.setCommission(rate.getRate() * card.getRelLimit());//手续费
                    bill.setBeginTime(card.getStmStatementDate());//账单日
                    bill.setEndTime(card.getStmRepaymentDate());//到期还款日
                    bill.setRemark("客户"+customer.getName()+DateUtils.formatDateyyyyMMdd(DateUtils.getCurrentUnixTime())+"账单");
                    int billId = billDao.insert(bill);//增加账单
                    logger.info("Method[opertationTask],billId[{}]",billId);
                    if(billId>0){
                        RemindPayTask payTask = new RemindPayTask();
                        payTask.setBlid(bill.getId());
                        payTask.setMoney(card.getRelLimit());//信用卡额度
                        payTask.setCommission(rate.getRate() * card.getRelLimit());
                        payTask.setTimes(1);
                        payTask.setStatus(0);//0 :待催收 1: 催收中 2，催收成功  -1, 坏账
                        payTask.setCreateTime(DateUtils.getCurrentUnixTime());
                        payTask.setUpdateTime(DateUtils.getCurrentUnixTime());
                        int flag = remindPayTaskDao.insert(payTask);
                        logger.info("Method[opertationTask],flag[{}]",billId);
                        if(flag==0){
                            logger.error("客户" + customer.getName()+card.getStmStatementDate()+"账单记录失败");
                            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        }else{
                            logger.info("客户" + customer.getName()+card.getStmStatementDate()+"账单记录成功");
                        }

                    }else {
                        logger.error("客户"+customer.getName()+card.getStmStatementDate()+"账单记录失败");
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();

                    }
                } else{
                    logger.info("客户Id"+card.getCid()+card.getStmStatementDate()+"- >>账单记录已存在");
                }


            }

        }catch (Exception ex){
            logger.info("账单记录失败");
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }



    }
}
